package com.cg.big_event.service.impl;

import com.cg.big_event.entity.Article;
import com.cg.big_event.entity.PageBean;
import com.cg.big_event.mapper.ArticleMapper;
import com.cg.big_event.service.ArticleService;
import com.cg.big_event.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Override
    public void add(Article article) {
        article.setCreateTime( LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get ();
        String idStr = (String) map.get("id");
        Integer id = Integer.parseInt(idStr);
        article.setCreateUser(String.valueOf(id));
        articleMapper.add(article);
    }
    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize,Integer categoryId, String state) {
        PageBean<Article> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        Map<String, Object> map =ThreadLocalUtil.get ();
        String idStr = (String) map.get("id");
        Integer userId = Integer.parseInt(idStr);
        List<Article> as = articleMapper.list(userId,categoryId,state);
        Page<Article> p=(Page<Article>) as;
     pb.setTotal (p.getTotal());
     pb.setItems(p.getResult ());
     return pb;
    }
    @Override
    public Article detail(Integer id) {
        Article article = articleMapper.detail(id);
        return article;
    }
    @Override
    public void update(Article article) {
        articleMapper.update(article);
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }
}
